<template>
  <div class="download-resources">
    <el-tooltip
      effect="dark"
      placement="bottom"
      popper-class="popper-tooltip"
      :disabled="sceneStore.sceneDetail?.conifgId ? false : true"
    >
      <el-button
        :disabled="sceneStore.sceneDetail?.conifgId ? false : true"
        :type="sceneStore.sceneDetail?.conifgId ? 'primary' : 'info'"
        class="custom-button"
      >
        <span>导出</span>
        <template #icon>
          <SvgIcon icon-class="ic_发导出" />
        </template>
      </el-button>
      <template #content>
        <div class="download-btn" @click="downLoad('二次开发')">二次开发</div>
        <div class="download-btn" @click="downLoad('离线部署')">离线部署</div>
      </template>
    </el-tooltip>
  </div>

  <div class="dialog-content" v-if="dialogVisible">
    <div class="dialog-title">
      <img src="@/assets/png/waring.png" alt="" />
      <span>正在生成{{ title }}包</span>
      <img
        class="close-icon"
        @click="dialogVisible = false"
        src="@/assets/png/closeicon.png"
        alt=""
      />
    </div>
    <p class="await">请耐心等待...</p>
    <div class="look-down">
      <div class="look-text" @click="toDownload">到“下载管理”里面查看</div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { loadingResources } from '@/utils/download-package';
import { useSceneStore } from '@/store/scene';
import { useEditorStore } from '@/store/editor';

const editorStore = useEditorStore();
const sceneStore = useSceneStore();
const dialogVisible = ref(false);
const title = ref('');

const downLoad = async (type: string) => {
  await editorStore.saveScene(false);
  if (!sceneStore.sceneDetail?.conifgId) return;
  title.value = type;
  dialogVisible.value = true;
  await loadingResources(sceneStore.sceneDetail, type, showToast);
};

const showToast = (result: any) => {
  if (result) {
    dialogVisible.value = true;
  }
};

const toDownload = () => {
  window.open('/scene/download', '_blank');
};
</script>

<style lang="less" scoped>
.download-resources {
  margin-left: 10px;
  :deep(.el-button) {
    .el-icon {
      font-size: 20px !important;
      color: #545454;
    }
  }
}

.dialog-content {
  width: 420px;
  height: 126px;
  background: #2e2e2e;
  box-shadow: 0px 12px 48px 16px rgba(0, 0, 0, 0.03), 0px 9px 28px 0px rgba(0, 0, 0, 0.05),
    0px 6px 16px -8px rgba(0, 0, 0, 0.08);
  border-radius: 8px;
  padding: 16px 24px;
  position: fixed;
  top: 66px;
  right: 17px;
  box-sizing: border-box;
  .dialog-title {
    display: flex;
    align-items: center;
    font-size: 16px;
    font-weight: 500;
    img {
      width: 24px;
      height: 24px;
      margin-right: 16px;
    }
    .close-icon {
      position: absolute;
      right: 0px;
      width: 16px;
      height: 16px;
      top: 15px;
      cursor: pointer;
    }
  }
  .await {
    text-indent: 40px;
    font-size: 14px;
    font-weight: 400;
    color: rgba(255, 255, 255, 0.88);
    margin: 12px 0;
  }
  .look-down {
    display: flex;
    justify-content: flex-end;
    .look-text {
      width: 158px;
      height: 24px;
      background: #0560fd;
      border-radius: 2px;
      font-size: 12px;
      color: #ffffff;
      line-height: 24px;
      text-align: center;
      cursor: pointer;
    }
  }
}
</style>
